package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryWCL extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
    public QueryWCL() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );		
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		ResultTableSet res = null;
		ArrayList<I1S1> i1s1s = null;
		String sord = null;
		String sidx = null;
		
		int total_pages = 1;
		int count = 0; 
		int page = 1;
		int limit = 10;
		
		/*	決定執行動作型態
		 * 		1.	產生 grid
		 * 		2. 產生下拉吧
		 * 
		 * 	*/
		int actType = Integer.parseInt( request.getParameter("fc") );
		
		try {
			String strSQL = null;

			final String bssId = request.getParameter("bssId");
			String subSysId = request.getParameter("subSysId");
			
			
			if ( actType == 1 ) {
				strSQL = "SELECT COUNT(*) FROM wcl WHERE bssid=? AND subsysid=?";
				res = new ResultTableSet();
				sord = request.getParameter("sord");
				sidx = "webconsolesn";
				page = Integer.parseInt(request.getParameter("page"));
				limit = Integer.parseInt(request.getParameter("rows"));
			} else if ( actType == 2 ) {             
				strSQL = "SELECT webconsolesn, webconsoleip FROM wcl WHERE bssid=? AND subsysid=?";
				i1s1s = new ArrayList<I1S1>();
				sord = "ASC";
			}
			
			conn = wid.getConn(true);	
			ps = conn.prepareStatement(strSQL);
			ps.setString(1, bssId);
			ps.setString(2, subSysId);
			rs = ps.executeQuery();
			
			if ( actType == 1 ) {
				rs.next();
				count  = rs.getInt(1);
				
				if ( count  != 0 ) {
					if (limit  < count) {
						total_pages = (count / limit)+1;	
					}
					if ( conn.getMetaData().getDatabaseProductName().contains( "MySQL" ) ) {
						strSQL = "SELECT t1.webconsolesn, t1.webconsoleip,t1.modulepath,t1.loginid,t1.pwd,t2.bssid,t2.bssname,t3.subsysid,t3.subsysname FROM wcl t1,bss t2,subsys t3 WHERE t1.bssid=? AND t1.subsysid=? AND t1.bssid=t2.bssid AND t1.subsysid=t3.subsysid ORDER BY "+sidx+" "+sord+" LIMIT "+(limit*page-limit)+","+ limit;
					} else if ( conn.getMetaData().getDatabaseProductName().contains( "Informix" ) ) {
						strSQL = "SELECT SKIP "+(limit*page-limit)+" FIRST "+ limit +" t1.webconsolesn, t1.webconsoleip,t1.modulepath,t1.loginid,t1.pwd,t2.bssid,t2.bssname,t3.subsysid,t3.subsysname FROM wcl t1,bss t2,subsys t3 WHERE t1.bssid=? AND t1.subsysid=? AND t1.bssid=t2.bssid AND t1.subsysid=t3.subsysid ORDER BY "+sidx+" "+sord;
					}
					
					ps = conn.prepareStatement(strSQL);
					ps.setString(1, bssId);
					ps.setString(2, subSysId);
					rs = ps.executeQuery();
					ArrayList<Rows> rows = new ArrayList<Rows>();
					
					while ( rs.next() ) {
						Rows row = new Rows();
						String[] cell = null;
						row.setId( String.valueOf( rs.getString("webconsolesn") ) );
						cell = new String[5];
						cell[0] = String.valueOf( rs.getInt("webconsolesn") );
						cell[1] = rs.getString("webconsoleip");
						cell[2] = rs.getString("modulepath");
						cell[3] = rs.getString("loginid");
						cell[4] = rs.getString("pwd");
						
						row.setCell(cell);
						rows.add(row);
					}
					res.setRows(rows);
				} else {
					page =0;
					total_pages=0;
				}
				res.setPage(String.valueOf(page));
				res.setTotal(total_pages);
				res.setRecords(String.valueOf(count));
			} else if ( actType ==2 ) {
				I1S1 noSel = new I1S1();
				noSel.setIntA(0);
				noSel.setStrA("無設定");
				i1s1s.add(noSel);
				
				while ( rs.next() ) {
					I1S1 i1s1 = new I1S1();
					i1s1.setIntA( rs.getInt(1) );
					i1s1.setStrA( rs.getString(2) );
					i1s1s.add(i1s1);
				}
			}
			
			
		} catch (SQLException e) {
			wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch (Exception e) {
			wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				System.out.println(wid.errProcess(e));
			}
		}
		
		if ( actType == 1 ) {
			response.getWriter().write( new Gson().toJson( res ) );	
		} else {
			response.getWriter().write( new Gson().toJson( i1s1s ) );
		}
	}
}